<template>
  <div 
    class="notification-toast" 
    :class="`toast-${type}`"
    @click="handleClick"
  >
    <div class="toast-icon">
      {{ toastIcon }}
    </div>
    <div class="toast-content">
      <p class="toast-message">{{ message }}</p>
    </div>
    <button class="toast-close" @click.stop="handleClose">
      ×
    </button>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted } from 'vue'

/**
 * 通知提示组件
 * 显示临时通知消息
 * @component
 * @example
 * <NotificationToast 
 *   message="操作成功" 
 *   type="success" 
 *   :duration="3000"
 *   @close="handleClose" 
 * />
 */

interface Props {
  message: string
  type?: 'success' | 'error' | 'warning' | 'info'
  duration?: number
  closable?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  type: 'info',
  duration: 3000,
  closable: true
})

const emit = defineEmits<{
  close: []
}>()

let autoCloseTimer: number | null = null

/**
 * 提示图标
 */
const toastIcon = computed(() => {
  switch (props.type) {
    case 'success':
      return '✅'
    case 'error':
      return '❌'
    case 'warning':
      return '⚠️'
    case 'info':
    default:
      return 'ℹ️'
  }
})

/**
 * 处理点击事件
 */
const handleClick = (): void => {
  if (props.closable) {
    handleClose()
  }
}

/**
 * 处理关闭事件
 */
const handleClose = (): void => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
    autoCloseTimer = null
  }
  emit('close')
}

/**
 * 设置自动关闭定时器
 */
const setAutoClose = (): void => {
  if (props.duration > 0) {
    autoCloseTimer = window.setTimeout(() => {
      handleClose()
    }, props.duration)
  }
}

// 生命周期钩子
onMounted(() => {
  setAutoClose()
})

onUnmounted(() => {
  if (autoCloseTimer) {
    clearTimeout(autoCloseTimer)
  }
})
</script>

<style lang="scss" scoped>
.notification-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: $spacing-md;
  padding: $spacing-md $spacing-lg;
  background: $bg-primary;
  border-radius: $border-radius-lg;
  box-shadow: $shadow-lg;
  border-left: 4px solid;
  max-width: 400px;
  min-width: 300px;
  z-index: 1100;
  cursor: pointer;
  animation: slideInRight 0.3s ease-out;

  &.toast-success {
    border-left-color: $success-color;
    background: rgba($success-color, 0.05);
  }

  &.toast-error {
    border-left-color: $danger-color;
    background: rgba($danger-color, 0.05);
  }

  &.toast-warning {
    border-left-color: $warning-color;
    background: rgba($warning-color, 0.05);
  }

  &.toast-info {
    border-left-color: $secondary-color;
    background: rgba($secondary-color, 0.05);
  }

  .toast-icon {
    font-size: 1.5rem;
    flex-shrink: 0;
  }

  .toast-content {
    flex: 1;

    .toast-message {
      color: $text-color;
      margin: 0;
      font-size: 0.9rem;
      line-height: 1.4;
    }
  }

  .toast-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: $text-light;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: $transition-fast;
    flex-shrink: 0;

    &:hover {
      background: rgba(0, 0, 0, 0.1);
      color: $text-color;
    }
  }

  &:hover {
    transform: translateX(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  }
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

// 响应式设计
@media (max-width: $breakpoint-sm) {
  .notification-toast {
    top: 10px;
    right: 10px;
    left: 10px;
    max-width: none;
    min-width: auto;
  }
}
</style>